import { builtinModules } from 'node:module';

import eslint from '@eslint/js';
import { defineConfig } from 'eslint/config';
import eslintConfigPrettier from 'eslint-config-prettier/flat';
import playwright from 'eslint-plugin-playwright';
import reactPlugin from 'eslint-plugin-react';
import reactHooksPlugin from 'eslint-plugin-react-hooks';
import simpleImportSortPlugin from 'eslint-plugin-simple-import-sort';
import eslintPluginUnicorn from 'eslint-plugin-unicorn';
import globals from 'globals';
import tseslint from 'typescript-eslint';

export default defineConfig([
  // https://typescript-eslint.io/getting-started#additional-configs
  eslint.configs.recommended,
  tseslint.configs.strict,
  tseslint.configs.stylistic,
  // Unicorn section
  eslintPluginUnicorn.configs.unopinionated,
  {
    rules: {
      'unicorn/filename-case': [
        'error',
        {
          case: 'kebabCase',
        },
      ],
      'unicorn/no-for-loop': 'error', //helps avoid off-by-one errors
      'unicorn/prefer-top-level-await': 'off', // no top level await in our build targets yet
      'unicorn/no-process-exit': 'off', // we have a CLI app
      'unicorn/switch-case-braces': 'error', // more explicit braces
      'unicorn/no-array-method-this-argument': 'off', //our nedb implementation uses this
      'unicorn/prefer-dom-node-text-content': 'off', // we use this in an e2e test
      'unicorn/prefer-response-static-json': 'off', // unsafe in our templating worker

      'unicorn/no-array-for-each': 'off', // TODO: delete me
      'unicorn/no-array-reverse': 'off', // TODO: delete me
      'unicorn/no-array-sort': 'off', // TODO: delete me
      'unicorn/no-negated-condition': 'off', // TODO: delete me
      'unicorn/no-object-as-default-parameter': 'off', // TODO: delete me
      'unicorn/no-this-assignment': 'off', // TODO: delete me
      'unicorn/no-zero-fractions': 'off', // TODO: delete me
      'unicorn/prefer-add-event-listener': 'off', // TODO: delete me
      'unicorn/prefer-array-some': 'off', // TODO: delete me
      'unicorn/prefer-at': 'off', // TODO: delete me -
      'unicorn/prefer-global-this': 'off', // TODO: delete me
      'unicorn/prefer-logical-operator-over-ternary': 'off', // TODO: delete me
      'unicorn/prefer-regexp-test': 'off', // TODO: delete me
      'unicorn/prefer-set-has': 'off', // TODO: delete me
      'unicorn/prefer-string-raw': 'off', // TODO: delete me
      'unicorn/prefer-string-replace-all': 'off', // TODO: delete me
      'unicorn/prefer-switch': 'off', // TODO: delete me
    },
  },
  // Playwright section
  {
    ...playwright.configs['flat/recommended'],
    files: ['packages/insomnia-smoke-test/tests/**/*.ts'],
    plugins: { playwright: playwright },
    rules: {
      ...playwright.configs['flat/recommended'].rules,
      'playwright/expect-expect': 'off',
      'playwright/require-soft-assertions': 'error',
      'playwright/prefer-native-locators': 'error',
      'playwright/prefer-to-be': 'error',
      'playwright/prefer-to-contain': 'error',
      'playwright/no-wait-for-timeout': 'error',
    },
  },
  // nodeIntegration: false section
  {
    files: [
      'packages/insomnia/src/ui/**/*.{ts,tsx}',
      // TODO: 'packages/insomnia/src/common/**/*.{ts,tsx}',
    ],
    rules: {
      'no-restricted-imports': [
        'error',
        {
          paths: builtinModules.map(m => `node:${m}`),
        },
      ],
    },
  },
  // React hooks section
  {
    files: ['packages/insomnia/src/**/*.{ts,tsx}'],
    plugins: { 'react-hooks': reactHooksPlugin },
    rules: {
      ...reactHooksPlugin.configs.recommended.rules,
      'react-hooks/refs': 'off', //TODO: delete me
      'react-hooks/set-state-in-effect': 'off', //TODO: delete me
      'react-hooks/immutability': 'off', //TODO: delete me
      'react-hooks/preserve-manual-memoization': 'off', //TODO: delete me
      'react-hooks/incompatible-library': 'off', //TODO(use react-aria virtualizer): delete me
    },
  },
  // React section
  {
    files: ['packages/insomnia/src/**/*.{ts,tsx}'],
    ...reactPlugin.configs.flat.recommended,
    ...reactPlugin.configs.flat['jsx-runtime'],
    languageOptions: {
      ...reactPlugin.configs.flat.recommended.languageOptions,
      globals: {
        ...globals.browser,
      },
    },
    settings: {
      react: {
        version: 'detect',
      },
    },
    rules: {
      'react/jsx-first-prop-new-line': ['error', 'multiline'],
      'react/jsx-max-props-per-line': ['error', { maximum: 1, when: 'multiline' }],
      'react/jsx-indent-props': ['error', 2],
      'react/function-component-definition': [
        'error',
        {
          namedComponents: ['arrow-function', 'function-declaration'],
          unnamedComponents: 'arrow-function',
        },
      ],
      'react/jsx-closing-bracket-location': ['error', 'line-aligned'],
      'react/prefer-stateless-function': 'error',
      'react/jsx-key': ['error', { checkFragmentShorthand: true }],
      'react/self-closing-comp': 'error',
      'react/react-in-jsx-scope': 'off',
      'react/no-unescaped-entities': 'off',
      'react/prop-types': 'off',
      'react/no-array-index-key': 'error',
    },
  },
  // simple-import-sort section
  {
    plugins: {
      'simple-import-sort': simpleImportSortPlugin,
    },
    rules: {
      'simple-import-sort/imports': 'error',
    },
  },
  // General ESLint rules
  {
    rules: {
      'no-restricted-imports': [
        'error',
        {
          patterns: [
            // Shouldn't import packages by relative path
            {
              group: ['**/*/insomnia-api/**'],
              message: "Please use 'insomnia-api' instead of relative paths",
            },
          ],
        },
      ],
    },
  },
  {
    rules: {
      'default-case': 'error',
      'default-case-last': 'error',
      'eqeqeq': ['error', 'smart'],
      'no-async-promise-executor': 'off',
      'no-else-return': 'error',
      'no-empty': ['error', { allowEmptyCatch: true }],
      'no-var': 'error',
      'no-inner-declarations': 'off',
      'no-useless-escape': 'off', // TODO: delete me
    },
  },
  // TypeScript ESLint rules
  {
    rules: {
      '@typescript-eslint/array-type': ['error', { default: 'array', readonly: 'array' }],
      '@typescript-eslint/consistent-type-definitions': ['error', 'interface'],
      '@typescript-eslint/consistent-type-imports': 'error',
      '@typescript-eslint/no-empty-object-type': 'off', // TODO: delete me
      '@typescript-eslint/no-empty-function': 'off',
      '@typescript-eslint/no-namespace': ['error', { allowDeclarations: true }],
      '@typescript-eslint/no-redeclare': 'error',
      '@typescript-eslint/no-require-imports': 'off',
      '@typescript-eslint/no-unused-expressions': 'off', // TODO: delete me
      '@typescript-eslint/no-unused-vars': 'off', // TODO: delete me

      '@typescript-eslint/no-use-before-define': 'off', // TODO: delete me
      '@typescript-eslint/no-explicit-any': 'off', // TODO: delete me
      '@typescript-eslint/no-dynamic-delete': 'off',
      '@typescript-eslint/no-non-null-assertion': 'off',
      '@typescript-eslint/no-invalid-void-type': 'off',
    },
  },
  eslintConfigPrettier,
  {
    ignores: [
      '*.md',
      '**/__fixtures__/*',
      '**/__snapshots__/*',
      '**/.cache/*',
      '**/.github/*',
      '**/.idea/*',
      '**/*.config.js',
      '**/*.d.ts',
      '**/*.min.js',
      '**/*.js.map',
      '**/bin/*',
      '**/build/*',
      '**/coverage/*',
      '**/customSign.js',
      '**/dist/*',
      '**/docker/*',
      '**/electron/index.js',
      '**/fixtures',
      '**/node_modules/*',
      '**/svgr',
      '**/traces/*',
      '**/verify-pkg.js',
      '**/__mocks__/*',
      '**/.react-router/*',
    ],
  },
]);
